﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using KiraSh.ResumeTracker.Properties;

namespace KiraSh.ResumeTracker
{
    partial class frmDisplayJobs : Form
    {
        private static frmDisplayJobs instance = null;
        private Company currentCompany = null;

        // there were no jobs in the profile
        private bool noJobs = false;
        // values for word
        Object refmissing = System.Reflection.Missing.Value;
        // page index
        int page = 1;
        // number of resumes per page
        int numPPage = Settings.Default.maxResumesPerPage;
        // list of jobs for the company
        List<ResumeJob> jobs;
        // binding source for page counter
        BindingSource src;

        public static frmDisplayJobs ShowForm()
        {
            // if currently opened DisplayJobs form is not for the company requested,
            // then close the window
            if (instance!=null && !CurrentContext.Company.Equals(instance.currentCompany))
            {
                instance.Close();
                instance = null;
            }
            // if instance is null or it was disposed -> create new window
            if (instance == null || instance.IsDisposed)
            {
                instance = new frmDisplayJobs();
            }

            // show the instance and bring it in front of the other forms
            instance.Show();
            instance.BringToFront();

            return instance;
        }

        private frmDisplayJobs()
        {
            currentCompany = CurrentContext.Company;
            InitializeComponent();
            this.Text = CurrentContext.Name + " | Profile: " + CurrentContext.Profile.Name + " | Company: " + CurrentContext.Company.Name;

        }

        // load applications
        private void frmDisplayJobs_Load(object sender, EventArgs e)
        {
            XMLDealer xml = new XMLDealer();
            jobs = new List<ResumeJob>();
            src = new BindingSource();
            // load whatever is in the list of applications
            object o = xml.LoadXML(PathHelper.MapCombine(CurrentContext.Company.Path), typeof(List<ResumeJob>));
            if (o != null)
            {
                jobs = (List<ResumeJob>)o;
                // sort jobs by date
                jobs.Sort();
                // if there are no jobs to display - load the add jobs window
                if (jobs.Count < 1)
                {
                    noJobs = true;
                }
                else
                {
                    addJobs(jobs);
                }
            }
            else
            {
                noJobs = true;
            }
            // the number of pages needed for to display all the applications 
            // based on the number of applications per page set up in config
            int pageNum = jobs.Count / numPPage + (jobs.Count % numPPage > 0 ? 1 : 0);
            // the size of the source corresponds to the number of the pages needed
            src.DataSource = new byte[pageNum];
            bndPageCount.BindingSource = src;
            // add page flip
            src.PositionChanged += new EventHandler(src_PositionChanged);
        }

        // the page was changed need to update the appplications displayed
        private void src_PositionChanged(object sender, EventArgs e)
        {
            // get page number
            page = int.Parse(bndPageCount.PositionItem.Text);
            // get rid of the table and release all the files
            dispose_table();
            // display applications
            addJobs(jobs);
        }

        // display applications for the current page (page) and base 
        // on the number of pages set up in config
        private void addJobs(List<ResumeJob> jobs)
        {
            bool alter = true;

            // display the jobs in the list fpr the current page
            for (int i = (page - 1) * numPPage; i < Math.Min(page * numPPage, jobs.Count); i++)
            {
                ResumeJob job = jobs[i];
                this.tableLayoutPanel1.RowCount = this.tableLayoutPanel1.RowCount + 1;
                System.Windows.Forms.Panel pnlInfo;
                System.Windows.Forms.Label lblJob;
                System.Windows.Forms.LinkLabel lblLink;
                System.Windows.Forms.Label lblSentTo;
                System.Windows.Forms.Label lblDate;
                System.Windows.Forms.Panel pnlPosting;
                System.Windows.Forms.Panel pnlResume;
                AxSHDocVw.AxWebBrowser axPosting;
                AxSHDocVw.AxWebBrowser axResume;
                Color bgColor = alter ? System.Drawing.SystemColors.Control : System.Drawing.SystemColors.ControlLight;
                alter = !alter;

                addRow(out pnlInfo, out lblJob, out lblLink, out lblSentTo,
                    out lblDate, out pnlPosting, out pnlResume, out axPosting, out axResume, bgColor);

                lblDate.Text = lblDate.Text + job.Date.ToShortDateString();
                lblJob.Text = lblJob.Text + job.JobName;
                lblLink.Text = lblLink.Text + job.JobLink;
                lblSentTo.Text = lblSentTo.Text + job.SendTo;
                OpenWordDoc(job.JobPath, axPosting);
                OpenWordDoc(job.ResumePath, axResume);

            }
            // release everything that might be already there
            tableLayoutPanel1.RowStyles.Clear();
            // add new rows
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 400));
            }
            // update the size of the table
            Size old = this.Size;
            this.Size = new Size(this.Size.Width + 100, this.Size.Height + 1);
            this.Size = old;
        }

        // loads word doc to the specified field
        private void OpenWordDoc(string path, AxSHDocVw.AxWebBrowser obj)
        {
            obj.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(NavigateComplete);
            obj.Navigate(path, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
        }

        // add row with job application to the table
        private void addRow(
            out System.Windows.Forms.Panel pnlInfo,
            out System.Windows.Forms.Label lblJob,
            out System.Windows.Forms.LinkLabel lblLink,
            out System.Windows.Forms.Label lblSentTo,
            out System.Windows.Forms.Label lblDate,
            out System.Windows.Forms.Panel pnlPosting,
            out System.Windows.Forms.Panel pnlResume,
            out AxSHDocVw.AxWebBrowser axPosting,
            out AxSHDocVw.AxWebBrowser axResume,
            Color bgColor)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDisplayJobs));

            #region Addition of the row layout

            pnlInfo = new System.Windows.Forms.Panel();
            lblLink = new System.Windows.Forms.LinkLabel();
            lblSentTo = new System.Windows.Forms.Label();
            lblDate = new System.Windows.Forms.Label();
            lblJob = new System.Windows.Forms.Label();
            pnlPosting = new System.Windows.Forms.Panel();
            pnlResume = new System.Windows.Forms.Panel();
            axPosting = new AxSHDocVw.AxWebBrowser();
            axResume = new AxSHDocVw.AxWebBrowser();
            tableLayoutPanel1.SuspendLayout();
            pnlInfo.SuspendLayout();
            pnlPosting.SuspendLayout();
            pnlResume.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(axPosting)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(axResume)).BeginInit();
            this.SuspendLayout();

            tableLayoutPanel1.Controls.Add(pnlInfo, 0, this.tableLayoutPanel1.RowCount - 1);
            tableLayoutPanel1.Controls.Add(pnlPosting, 1, this.tableLayoutPanel1.RowCount - 1);
            tableLayoutPanel1.Controls.Add(pnlResume, 2, this.tableLayoutPanel1.RowCount - 1);

            // 
            // pnlInfo
            // 
            pnlInfo.BackColor = bgColor;
            pnlInfo.DoubleClick += new EventHandler(pnlInfo_DoubleClick);
            pnlInfo.Controls.Add(lblLink);
            pnlInfo.Controls.Add(lblSentTo);
            pnlInfo.Controls.Add(lblDate);
            pnlInfo.Controls.Add(lblJob);
            pnlInfo.Dock = System.Windows.Forms.DockStyle.Fill;
            pnlInfo.Location = new System.Drawing.Point(3, 3);
            pnlInfo.Name = "pnlInfo";
            pnlInfo.TabIndex = 0;
            // 
            // lblLink
            // 
            lblLink.AutoSize = true;
            lblLink.LinkColor = System.Drawing.Color.Black;
            lblLink.Location = new System.Drawing.Point(9, 69);
            lblLink.Name = "lblLink";
            lblLink.TabIndex = 3;
            lblLink.TabStop = true;
            lblLink.Text = "Link: ";
            lblLink.VisitedLinkColor = System.Drawing.Color.Black;
            // 
            // lblSentTo
            // 
            lblSentTo.AutoSize = true;
            lblSentTo.Location = new System.Drawing.Point(9, 46);
            lblSentTo.Name = "lblSentTo";
            lblSentTo.TabIndex = 2;
            lblSentTo.Text = "Sent to: ";
            // 
            // lblDate
            // 
            lblDate.AutoSize = true;
            lblDate.Location = new System.Drawing.Point(9, 23);
            lblDate.Name = "lblDate";
            lblDate.TabIndex = 1;
            lblDate.Text = "Date: ";
            // 
            // lblJob
            // 
            lblJob.AutoSize = true;
            lblJob.Location = new System.Drawing.Point(9, 0);
            lblJob.Name = "lblJob";
            lblJob.TabIndex = 0;
            lblJob.Text = "Job: ";
            // 
            // pnlPosting
            // 
            pnlPosting.AutoSize = true;
            pnlPosting.Controls.Add(axPosting);
            pnlPosting.Dock = System.Windows.Forms.DockStyle.Fill;
            pnlPosting.Name = "pnlPosting";
            pnlPosting.TabIndex = 1;
            // 
            // pnlResume
            // 
            pnlResume.AutoSize = true;
            pnlResume.Controls.Add(axResume);
            pnlResume.Dock = System.Windows.Forms.DockStyle.Fill;
            pnlResume.Name = "pnlResume";
            pnlResume.TabIndex = 2;
            // 
            // axPosting
            // 
            axPosting.Dock = System.Windows.Forms.DockStyle.Fill;
            axPosting.Enabled = true;
            axPosting.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPosting.OcxState")));
            axPosting.TabIndex = 0;
            // 
            // axResume
            // 
            axResume.Dock = System.Windows.Forms.DockStyle.Fill;
            axResume.Enabled = true;
            axResume.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axResume.OcxState")));
            axResume.TabIndex = 0;

            tableLayoutPanel1.ResumeLayout(false);
            tableLayoutPanel1.PerformLayout();
            pnlInfo.ResumeLayout(false);
            pnlInfo.PerformLayout();
            pnlPosting.ResumeLayout(false);
            pnlResume.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(axPosting)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(axResume)).EndInit();
            this.ResumeLayout(false);

            #endregion
        }

        // double click in the empty field adds new application
        private void pnlInfo_DoubleClick(object sender, EventArgs e)
        {
            // open add application form
            frmAddJobsResumes frm = new frmAddJobsResumes();
            frm.Show();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
        }

        // new application might have been added
        private void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            // new application was added and saved
            if (((frmAddJobsResumes)sender).Saved)
            {
                // release any current resources used by 
                // the table and reload
                dispose_table();
                frmDisplayJobs_Load(sender, e);
            }
            instance = null;
            CurrentContext.Company = null;
        }

        // reale all the resources used by the table
        private void dispose_table()
        {
            foreach (Control c in tableLayoutPanel1.Controls)
            {
                c.Dispose();
            }
            // clear and dispose the rows of the table
            tableLayoutPanel1.Controls.Clear();
            tableLayoutPanel1.RowCount = 0;
        }

        // remove word toolbox
        private void NavigateComplete(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            ((AxSHDocVw.AxWebBrowser)sender).ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
            // change focus and scroll to the top of the page
            this.tableLayoutPanel1.Controls[0].Focus();
            this.VerticalScroll.Value = 0;
        }

        // load add jobs screen if there are no jobs for the given company
        private void frmDisplayJobs_Shown(object sender, EventArgs e)
        {
            if (noJobs)
            {
                pnlInfo_DoubleClick(null, null);
            }
        }

        // add master resume buttomn functionality
        private void btnAddMaster_Click(object sender, EventArgs e)
        {
            frmAddMaster.showForm();
        }

        // add about box button functionality
        private void tlsBtnAbout_Click(object sender, EventArgs e)
        {
            bxAbout.showForm();

        }

        private void btnEmail_Click(object sender, EventArgs e)
        {
            frmEmailForm.FormShow();
        }
    }

}
